package com.platform.modules.mall.activity.type;

import com.platform.common.exception.BusinessException;
import com.platform.common.utils.StringUtils;
import com.platform.modules.mall.entity.MallGoodsEntity;
import com.platform.modules.mall.entity.MallGoodsToActivityEntity;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Component;

import java.util.Iterator;


@Scope(value = "request" ,proxyMode = ScopedProxyMode.TARGET_CLASS)
@Component
public class DirectGoodsType extends ActivityTypeAbstract{

    private String goodsId;

    public void setData(String goodsId){
        if (StringUtils.isBlank(goodsId)){
            throw new BusinessException("商品id不能为空");
        }
        this.goodsId=goodsId;
    }

    @Override
    public MallGoodsToActivityEntity getEntity() {
        Iterator<MallGoodsToActivityEntity> iterator = entityList.iterator();
        while (iterator.hasNext()){
            MallGoodsToActivityEntity goods = iterator.next();
            if (goods.getGoodsId().equals(this.goodsId)){
                return goods;
            }
        }
        return null;
    }


    /**
     * 直接购买商品时，不存在后续操作，没有库存，下线，删除的商品直接抛出异常
     * @return
     */
    @Override
    public boolean delGoodsNum(String goodsId, Integer number) {
        MallGoodsEntity goods = mallGoodsService.shopping(goodsId, number, null);
        return goods !=null;
    }
}
